﻿// (c) Copyright 2013 Mike Riedel.
// This source is subject to the Microsoft Public License (Ms-PL).
// All other rights reserved.

using System;

namespace FreshMeat.Threading
{
   /// <summary>
   /// Simple class that releases a lock when it is disposed.
   /// </summary>
   public class LockContext : IDisposable
   {
      /// <summary>
      /// Instance that does nothing to avoid having to create any more than necessary.
      /// </summary>
      public static readonly LockContext Nothing = new LockContext(() => { });

      readonly Action disposeAction;

      /// <summary>
      /// Creates instance.
      /// </summary>
      /// <param name="disposeAction">Action to perform when disposed.</param>
      public LockContext(Action disposeAction)
      {
         this.disposeAction = disposeAction;
      }

      /// <summary>
      /// Performs the specified action.
      /// </summary>
      public void Dispose()
      {
         if (disposeAction != null)
            disposeAction();
      }
   }
}